package com.niit.view;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.niit.entity.Book;
import com.niit.listener.MyMouseListenerAdapter;
import com.niit.listener.UpdateListener;
import com.niit.utils.Constants;

public class AddBookView extends JFrame {

	private JPanel centerPanel;
	private JLabel titleLabel,bookNameLabel, bookPriceLabel, bookPublisherLabel, bookAuthorLabel;
	private JTextField bookNameTextField, bookPriceTextField, bookPublisherTextField, bookAuthorTextField;
	private JButton saveBtn,cancelBtn;
	private String title;
	private MyMouseListenerAdapter listener;
	private Book book;

	public Book getBook() {
		return book;
	}

	public AddBookView(String title,MyMouseListenerAdapter listener) {
		this.title = title;
		this.listener = listener;
		init();
		showView();
		if(listener instanceof UpdateListener){
			//调用此方法实现数据回显
			dataBack();
		}
	}

	private void dataBack() {
		System.out.println("数据回显");
		UpdateListener listener = (UpdateListener)this.listener;
		book = listener.getB();
		System.out.println(book);
		bookNameTextField.setText(book.getBookName());
		bookPriceTextField.setText(book.getBookPrice().toString());
		bookPublisherTextField.setText(book.getBookPublisher());
		bookAuthorTextField.setText(book.getBookAuthor());
	}

	private void showView() {
		titleLabel.setFont(new Font(Constants.FONT_STYLE,Font.ROMAN_BASELINE,22));
		titleLabel.setBounds(150, 20, 100, 30);
		
		bookNameLabel.setBounds(20, 60, 80, 30);
		centerPanel.add(bookNameLabel);
		bookPriceLabel.setBounds(20, 100, 80, 30);
		centerPanel.add(bookPriceLabel);
		bookAuthorLabel.setBounds(20,140, 80, 30);
		centerPanel.add(bookAuthorLabel);
		bookPublisherLabel.setBounds(20, 180, 80, 30);
		centerPanel.add(bookPublisherLabel);
		
		bookNameTextField.setBounds(100, 60, 260, 30);
		centerPanel.add(bookNameTextField);
		bookPriceTextField.setBounds(100, 100, 260, 30);
		centerPanel.add(bookPriceTextField);
		bookAuthorTextField.setBounds(100, 180, 260, 30);
		centerPanel.add(bookAuthorTextField);
		bookPublisherTextField.setBounds(100,140, 260, 30);
		centerPanel.add(bookPublisherTextField);
		
		
		saveBtn.setBounds(140, 220, 70, 30);
		centerPanel.add(saveBtn);
		cancelBtn.setBounds(220, 220, 70, 30);
		cancelBtn.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				//点击取消后
				cancle();
			}
		});
		centerPanel.add(cancelBtn);
		listener.setView(this);
		saveBtn.addMouseListener(listener);
		
		this.add(titleLabel);
		this.add(centerPanel);
		this.setVisible(true);
	}

	protected void cancle() {
		this.dispose();
		new MainView(null);
	}

	private void init() {
		this.setTitle(title);
		this.setSize(400, 300);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		centerPanel = new JPanel(null);
		
		bookNameLabel = new JLabel("图书名称: ");
		bookPriceLabel = new JLabel("价格:");
		bookPublisherLabel = new JLabel("出版社:");
		bookAuthorLabel = new JLabel("作者:");
		titleLabel = new JLabel("新增图书");

		bookNameTextField = new JTextField();
		bookPriceTextField = new JTextField();
		bookPublisherTextField = new JTextField();
		bookAuthorTextField = new JTextField();

		saveBtn = new JButton("提交");
		cancelBtn = new JButton("取消");
		
	}

	public JTextField getBookNameTextField() {
		return bookNameTextField;
	}

	public JTextField getBookPriceTextField() {
		return bookPriceTextField;
	}

	public JTextField getBookPublisherTextField() {
		return bookPublisherTextField;
	}

	public JTextField getBookAuthorTextField() {
		return bookAuthorTextField;
	}
}
